package com.example.wyy;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import util.BlurUtil;
import util.MergeImage;

public class playActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout relativeLayout;
    private RelativeLayout back_grround;
    private ObjectAnimator animator;
    private ImageView next_music;
    private ImageView prev_music;
    private ImageView return_back;
    private ImageView music_paly_pause;
    private TextView play_title;
    private ImageView needle_music;
    private ImageView rotate;
    private ObjectAnimator needleAnimator;
    private TextView start_time;
    private TextView end_time;
    private SeekBar seekBar;
    int current_time;
    int total_time;
    boolean isAnimation = true; // 用来判断进度条是否在走

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_play);
        play_title = findViewById(R.id.play_title);


        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String artlist = intent.getStringExtra("artlist");
        //Log.e("TAG", "onCreate: " + title );
        current_time = intent.getIntExtra("current_time",0);
        total_time = intent.getIntExtra("total_time",0);
        Bitmap bi = intent.getParcelableExtra("bitmap");
        play_title.setText(title);

        //Log.e("TAG", "onCreate: " + current_time );
        //Log.e("TAG", "onCreate: " + total_time );

        next_music = findViewById(R.id.next_music);
        prev_music = findViewById(R.id.prev_music);
        return_back = findViewById(R.id.return_back);
        music_paly_pause = findViewById(R.id.music_play_pause);
        needle_music = findViewById(R.id.needle_music);
        rotate = findViewById(R.id.rotate);
        start_time = findViewById(R.id.start_time);
        end_time = findViewById(R.id.end_time);
        seekBar = findViewById(R.id.seekbar);
        back_grround = findViewById(R.id.back_ground);
        music_paly_pause.setOnClickListener(this);
        next_music.setOnClickListener(this);
        prev_music.setOnClickListener(this);
        return_back.setOnClickListener(this);

        end_time.setText(timeTransform(total_time));
        start_time.setText(timeTransform(current_time));
        seekBar.setMax(total_time);
        seekBar.setProgress(current_time);
        handler.post(runnable);

        if (start_time.getText().toString().equals(end_time.getText().toString()))
        {
            isAnimation = false;
            animator. end();
        }

        //Bitmap bit = BitmapFactory.decodeResource(getResources(),R.mipmap.img_12);
        Bitmap bitm = BlurUtil.doBlur(bi, 10, 10);
        back_grround.setBackground(new BitmapDrawable(bitm));
        //relativeLayout = findViewById(R.id.pan_rotate);
        //ImageView img = new ImageView(this);
        Bitmap pan_none = BitmapFactory.decodeResource(getResources(),R.mipmap.hgj);
        //Bitmap album = BitmapFactory.decodeResource(getResources(),R.mipmap.img_12);
        Bitmap bitmap = MergeImage.mergeThumbnailBitmap(pan_none,bi);
        //img.setImageBitmap(bitmap);
        rotate.setImageBitmap(bitmap);
        //relativeLayout.addView(img);

        //第一个参数是需要旋转的View，
        // 第二个是动画类型（包括alpha/rotation/scale/translate），
        // 第三个参数是旋转开始时的角度
        //第四个参数是旋转结束时的角度
        animator = ObjectAnimator.ofFloat(rotate, "rotation", 0f, 360.0f);
        //Log.e("TAG", "onCreate: " + needle_music.getHeight() );
        needle_music.setPivotX(160.0f);
        needle_music.setPivotY(100.0f);
        //needle_music.setPivotY(-10.0f);

        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());//匀速
        animator.setRepeatCount(-1);//设置动画重复次数（-1代表一直转）
        animator.setRepeatMode(ValueAnimator.RESTART);//动画重复模式
        animator.start();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.prev_music){

        }else if (v.getId() == R.id.next_music){

        }else if (v.getId() == R.id.return_back){
            finish();
        }else if (v.getId() == R.id.music_play_pause) {
            if (animator.isRunning()){
                isAnimation = false;
                music_paly_pause.setImageResource(R.mipmap.exq);
                needleAnimator = ObjectAnimator.ofFloat(needle_music,"rotation",0f,-30.0f);
                needleAnimator.start();
                animator.pause();
            }else {
                isAnimation = true;
                music_paly_pause.setImageResource(R.mipmap.exp);
                needleAnimator.reverse();
                // needleAnimator = ObjectAnimator.ofFloat(needle_music,"rotation",0f,30.0f);
                animator.resume();
                // needleAnimator.start();
            }
        }
    }
    private String timeTransform(int sec)
    {
        int minute = sec / 60;
        int second = sec - minute * 60;
        String mini,seco;
        return String.format((minute < 10? "0" + minute: minute+"") + ":"+ (second < 10 ? "0" + second : second ));
    }

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (isAnimation)
            {
                current_time = seekBar.getProgress();
                current_time++;
                seekBar.setProgress(current_time);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        start_time.setText(timeTransform(current_time));
                        handler.postDelayed(this,1000);
                    }
                });

                handler.postDelayed(this,1000);
            }
        }
    };

}